package com.atmilan.quartz.job;

import java.util.Date;

import com.atmilan.quartz.utils.product.JstProductUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.atmilan.quartz.constant.Constants;
import com.atmilan.quartz.constant.enums.SyncStatusEnum;
import com.atmilan.quartz.model.entity.crmeb.SystemConfig;
import com.atmilan.quartz.service.IJstSyncService;
import com.atmilan.quartz.service.ISystemConfigService;
import com.atmilan.quartz.utils.source.SourceJstProductUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

/**
 * 聚水潭数据推送
 * @author daigb
 * @date:  2022-02-23 10:07
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class JstSyncTask {
	@Autowired
    BaseTask baseTask;
    @Autowired
    JstProductUtil jstProductUtil;
    @Autowired
    IJstSyncService jstSyncService;
    @Autowired
    ISystemConfigService systemConfigService;
    
    /**
     * 同步聚水潭数据
     * @author daigb
     * @date:  2022-02-28 14:08
     */
    @XxlJob("jstSyncTask")
//    @Scheduled(fixedDelay = 1000 * 60 * 10 )
    public void syncTask() {
        try {
            SystemConfig systemConfig = systemConfigService.getOne(Wrappers.<SystemConfig>lambdaQuery()
                    .eq(SystemConfig::getName, Constants.JST_SYNC_STATUS)
                    .last("LIMIT 1")
                    );
            SyncStatusEnum jstSyncStatus = SyncStatusEnum.valueOf(systemConfig.getFormId());
            long timeDiff = new Date().getTime() - systemConfig.getUpdateTime().getTime();

            log.warn("--------------- 开始计划同步jst商品, 同步状态为: {} ---------------", jstSyncStatus);
            // 12小时内状态还未变更, 直接重新处理
            if (SyncStatusEnum.WAIT == jstSyncStatus && 12 < (timeDiff / 1000 / 60 / 60)) {
                log.warn("--------------- jst商品同步过程超过12小时, 自动重新拉取数据 ---------------");
                jstSyncStatus = SyncStatusEnum.PUSH;
            }
            // 有同步任务时不进行处理
            if (SyncStatusEnum.WAIT == jstSyncStatus) {
                log.warn("--------------- jst任务未结束, 新任务不执行 ---------------");
                return;
            }
            // 更新任务状态为同步中
            baseTask.updateSyscStatus(Constants.JST_SYNC_STATUS, SyncStatusEnum.WAIT, systemConfig);
            // 下发完成, 需要同步
            if (SyncStatusEnum.PUSH == jstSyncStatus) {
                boolean result = jstSyncService.syncProduct();
                if (result) {
                	baseTask.updateSyscStatus(Constants.JST_SYNC_STATUS, SyncStatusEnum.SYNC, systemConfig);
                    log.warn("--------------- jst商品拉取完成!!! ---------------");
                }
            // 同步完成, 需要下发
			} else if (SyncStatusEnum.SYNC == jstSyncStatus) {
				boolean result = jstSyncService.pushProduct();
				if (result) {
					baseTask.updateSyscStatus(Constants.JST_SYNC_STATUS, SyncStatusEnum.PUSH, systemConfig);
					log.warn("--------------- jst商品下发完成!!! ---------------");
				}
			}

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 刷新token 10天一次
     * @author daigb
     * @date:  2022-02-23 16:03
     */
    @XxlJob("jstRefreshTokenTask")
    public void jstTokenTask() {
    	try {
            jstProductUtil.refreshToken();
    		log.warn("--------------- 聚水潭刷新token完成!!! ---------------");
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }

}
